package com.lzx.applib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.lzx.applib.R;

/**
 * Created by Administrator on 16/11/27.
 */

public class SelectableImageView extends ImageView {

    int selectedRes;
    int unselectedRes;

    public SelectableImageView(Context context) {
        this(context, null);
    }

    public SelectableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SelectableImageView);
        selectedRes = array.getResourceId(R.styleable.SelectableImageView_selectedRes, 0);
        unselectedRes = array.getResourceId(R.styleable.SelectableImageView_unselectedRes, selectedRes);
        boolean selected = array.getBoolean(R.styleable.SelectableImageView_selected, false);
        array.recycle();

        setSelected(selected);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            if (selectedRes != 0) setImageResource(selectedRes);
        } else {
            if (unselectedRes != 0) setImageResource(unselectedRes);
        }
    }
}
